<template>
  <view class="container">
    <!-- 品牌头部区域 -->
    <view class="brand-header">
      <view class="brand-icon">
        <text class="icon">📱</text>
      </view>
      <view class="brand-info">
        <text class="app-name">SmartUI</text>
        <text class="page-desc">用户登录</text>
      </view>
      <view class="decor-line"></view>
    </view>
    
    <!-- 登录表单 -->
    <view class="form-container">
      <view class="input-group">
        <input 
          type="text" 
          v-model="username" 
          placeholder="请输入用户名" 
          class="input-field"
          @focus="onInputFocus('username')"
          @blur="onInputBlur('username')"
        />
      </view>
      
      <view class="input-group">
        <input 
          type="password" 
          v-model="password" 
          placeholder="请输入密码" 
          class="input-field"
          @focus="onInputFocus('password')"
          @blur="onInputBlur('password')"
        />
      </view>
      
      <button class="btn login-btn" @click="handleLogin">登录</button>
      
      <view class="link-container">
        <text>还没有账号？</text>
        <text class="link" @click="navToRegister">立即注册</text>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      username: '',
      password: '',
      focusedInput: ''
    };
  },
  methods: {
    onInputFocus(field) {
      this.focusedInput = field;
    },
    onInputBlur(field) {
      if (this.focusedInput === field) {
        this.focusedInput = '';
      }
    },
    handleLogin() {
      // 表单验证
      if (!this.username) {
        uni.showToast({ title: '请输入用户名', icon: 'none' });
        return;
      }
      
      if (!this.password) {
        uni.showToast({ title: '请输入密码', icon: 'none' });
        return;
      }
      
      // 模拟登录请求
      uni.showLoading({ title: '登录中...' });
      setTimeout(() => {
        uni.hideLoading();
        uni.showToast({ title: '登录成功', icon: 'success' });
        
        // 核心修改：登录成功后存储用户名到本地（持久化）
        uni.setStorageSync('userInfo', {
          username: this.username, // 存储输入的用户名
          isLogin: true // 标记已登录
        });
        
        setTimeout(() => {
          // 登录成功后跳转到新闻页面（tabBar页面用switchTab）
          uni.switchTab({
            url: '/pages/tabBar/NewsPage/NewsPage'
          });
        }, 1500);
      }, 1000);
    },
    navToRegister() {
      // 跳转到注册页面
      uni.navigateTo({
        url: '/pages/index/auth/register'
      });
    }
  }
};
</script>

<style scoped>
/* 原有样式不变，无需修改 */
.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f7fa;
  padding: 80rpx 30rpx 0;
}

.brand-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 80rpx;
  padding: 30rpx 0;
  width: 100%;
}

.brand-icon {
  width: 140rpx;
  height: 140rpx;
  border-radius: 50%;
  background: linear-gradient(135deg, #007aff 0%, #0051aa 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 30rpx;
  box-shadow: 0 8rpx 20rpx rgba(0, 122, 255, 0.25);
  transition: transform 0.3s ease;
}

.brand-icon:active {
  transform: scale(0.95);
}

.icon {
  font-size: 70rpx;
  color: #fff;
}

.app-name {
  font-size: 50rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 10rpx;
  display: block;
}

.page-desc {
  font-size: 26rpx;
  color: #666;
}

.decor-line {
  width: 80rpx;
  height: 6rpx;
  background-color: #007aff;
  border-radius: 3rpx;
  margin-top: 20rpx;
}

.form-container {
  width: 100%;
  max-width: 600rpx;
  background: #fff;
  padding: 60rpx;
  border-radius: 20rpx;
  box-shadow: 0 5rpx 20rpx rgba(0, 0, 0, 0.05);
}

.input-group {
  margin-bottom: 30rpx;
}

.input-field {
  width: 100%;
  height: 90rpx;
  padding: 0 25rpx;
  border: 1px solid #eee;
  border-radius: 15rpx;
  font-size: 30rpx;
  transition: all 0.3s ease;
}

.input-field:focus {
  border-color: #007aff;
  box-shadow: 0 0 0 2rpx rgba(0, 122, 255, 0.2);
  outline: none;
}

.btn {
  width: 100%;
  height: 96rpx;
  line-height: 96rpx;
  font-size: 34rpx;
  border-radius: 15rpx;
  margin-bottom: 30rpx;
  transition: all 0.3s ease;
}

.login-btn {
  background-color: #007aff;
  color: #fff;
  border: none;
}

.login-btn:active {
  background-color: #0051aa;
  transform: scale(0.98);
}

.link-container {
  display: flex;
  justify-content: center;
  font-size: 28rpx;
  color: #666;
  margin-top: 20rpx;
}

.link {
  color: #007aff;
  margin-left: 10rpx;
  text-decoration: underline;
  font-weight: 500;
}
</style>